package it.globalgamejam.scar.game.world 
{
	import it.globalgamejam.scar.Assets;
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.graphics.Spritemap;
	/**
	 * ...
	 * @author N1xx1
	 */
	public class Catapult extends Entity
	{
		private var dir:int = 0;  // 0=top, 1=right, 2=bottom, 3=left
		private static var deltaTime:Number = 0;
		
		private var spritemap:Spritemap = new Spritemap(Assets.CATAPULT, 30, 60);
		
		public function Catapult(_dir:int, x:Number=0, y:Number=0)
		{
			super(x + 59, y + 59);
			
			dir = _dir;
			
			spritemap.add("throw", [0, 1, 2, 3, 4, 3, 2, 1, 0], 18, false);
			spritemap.scaledHeight = 200;
			spritemap.scaledWidth = 100;
			spritemap.originX = 15;
			spritemap.originY = 45;
			spritemap.x = 0;
			spritemap.y = 0;
			
			if (dir == 0) spritemap.angle = 0;
			else if (dir == 1) spritemap.angle = 90;
			else if (dir == 2) spritemap.angle = 180;
			else if (dir == 3) spritemap.angle = 270;
			
			graphic = spritemap;
			
			setHitbox(3, 3);
			layer = 700;
		}
		
		public function setSpawnPos(_x:Number, _y:Number):void {
			x = _x + 59;
			y = _y + 59;
		}
		
		public static function createCatapult(dir:int):Catapult {
			return new Catapult(dir);
		}
		
		public override function update():void {
			super.update();
			
			deltaTime += FP.elapsed;
			
			if (deltaTime >= 5) {
				deltaTime -= 5;
				
				spritemap.play("throw", true);
				var colliding:Entity = collide("ball", x, y);
				if (!(colliding == null)) colliding = collide("player", x, y);
				if (!(colliding == null)) colliding = collide("rock", x, y);
				
				trace(colliding);
				if (colliding) {
					if ("launch" in colliding) {
						Function(colliding["launch"]).apply(colliding, dir);
					}
				}
			}
		}
	}

}